In order to set the email settings in PHP, you should configure the PHP correctly in the php.ini file. Look for the mail function section in the file and check for the two directives. If you are a Windows user, you have to make sure that the two directives are supplied correctly. SMTP, whose value specifies your email server address. Sendemail_ from, that specifies the email address of your own. You can make changes as below-
[mail function] //header in php.ini file ; For Win32 only. SMTP = smtp.secureserver.net ; For win32 only sendmail_from = webmaster@xxx.com
If you are using a Linux system then Sendmail application’s location should be known to PHP. To specify the path you need to make changes in the php.ini file and provide value for sendmail_path directive. You can make below changes-
[mail function] ; For Win32 only. SMTP = ; For win32 only sendmail_from = ; For Unix only sendmail_path = /usr/sbin/sendmail -t -i
How to send email using PHP
PHP supports mail() function that will allow you to send the email. This function takes five parameters, out of which three are mandatory that includes the recipient’s email address, the subject line and the content of the email. The other two parameters are optional and can be ignored while calling the function. First is headers which may specify the additional header like Cc or Bcc. Second is a parameter which may include an additional parameter for the email.
Syntax:
mail( to, subject, message, headers, parameters );
If the mail is sent successfully then the function will return true else return fail. You can even use multiple recipients as the first argument.
Example
<html> <head> <title>Sending email</title> </head> <body> <?php $to = "abc@somedomain.com"; $subject = "email subject"; $message = "<b>First Email.</b>"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $email_val = mail ($to,$subject,$message,$header); if( $email_val == true ) { echo "email sent successfully..."; }else { echo "email could not be sent..."; } ?> </body> </html>
How to send an email with an attachment
You can set content-type header to multipart/mixed if you want to use mixed content while sending the email.
Example:
<html> <head> <title>Sending email</title> </head> <body> <?php $to = "abc@somedomain.com"; $subject = "email subject"; $message = "<b>First Email.</b>"; $header .= "MIME-Version: 1.0\r\n"; // $header .= "Content-type: text/html\r\n"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_val = mail ($to,$subject,$message,$header); if( $email_val == true ) { echo "email sent successfully..."; }else { echo "email could not be sent..."; } ?> </body> </html>
People are also reading: